import java.util.Scanner;

public class demo03 {
    public static int max=0;
    public static int[] arr;
    //小朋友崇拜圈
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        arr=new int[n+1];
        for (int i = 1; i <= n; i++) {
            arr[i]=sc.nextInt();
        }
        for (int i = 1; i <= n; i++) {
            int count = 0;
            dfs(arr, i , count);
        }
        System.out.println(max);
    }
    public static void dfs(int[] arr,int index,int count){
        int num=arr[index];
        count++;
        while (num!=index){
            if(count>arr.length) {
                break;
            }
            num=arr[num];
            count++;
        }
        if(num==index){
            if (count > max) {
                max = count;
            }
        }
    }
}
